package com.event.usc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.event.usc.dto.EventAbb;
import com.event.usc.oracleJDBC.DatabaseHandler;
import com.google.gson.Gson;

/**
 * Servlet implementation class GeoServlet
 */
@WebServlet("/GeoServlet")
public class GeoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String qtype = request.getParameter("q");
		double lat = Double.parseDouble(request.getParameter("lat"));
		double lng = Double.parseDouble(request.getParameter("lng"));
		if (qtype.equals("range"))
		{
			double radius = Double.parseDouble(request.getParameter("radius"));
			doRangeRequest(lat, lng, radius, response);
		}
		else if (qtype.equals("knn"))
		{
			int k = Integer.parseInt(request.getParameter("k"));
			doKNNRequest(lat, lng, k, response);
		}
	}

	private void doKNNRequest(double lat, double lng, int k,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<EventAbb> result = handler.getKNNEventAbbs(lat, lng, k, getTime());          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void doRangeRequest(double lat, double lng, double radius, HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<EventAbb> result = handler.getRangeEventAbbs(lat, lng, radius, getTime());          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void printResponse(HttpServletResponse response,
			List<EventAbb> result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.write(getOutString(result));
		
	}

	private String getOutString(List<EventAbb> result) {
		String outstring;
		if (result==null || result.size()==0)
        	outstring = "[]";
        else
        {
        	Gson gson = new Gson();
        	outstring = gson.toJson(result);
        }
		return outstring;
	}
	
	private long getTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 1);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTimeInMillis();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
